package com.example.administrator.reader.element;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.administrator.reader.R;
import com.example.administrator.reader.entity.Novel;
import com.example.administrator.reader.entity.RequestStatus;
import com.example.administrator.reader.fragment.CityFragment;
import com.example.administrator.reader.fragment.ShelfFragment;
import com.example.administrator.reader.fragment.UserFragment;
import com.example.administrator.reader.request.BaseRequest;
import com.example.administrator.reader.request.BookAddRequest;
import com.example.administrator.reader.utils.Constants;
import com.example.administrator.reader.utils.UIUtils;
import com.example.administrator.reader.view.BookView;
import com.example.administrator.reader.view.SListView;

import java.util.List;


public class CZhudaElement extends BaseElement<List<Novel>> {
    SListView lv;

    public CZhudaElement(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void initView() {
        view = View.inflate(UIUtils.getContext(), R.layout.e_zhuda, null);
        lv = (SListView) view.findViewById(R.id.lv);
    }

    @Override
    public void fillView() {
        lv.setAdapter(new ZhudaAdapter());
    }

    class ZhudaAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_zhuda, null);
                holder = new Holder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_book_cover);
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tvAuthor = (TextView) convertView.findViewById(R.id.tv_author);
                holder.tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
                holder.bookView = (BookView) convertView.findViewById(R.id.book);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            Novel novel = data.get(position);
            bu.display(holder.iv, novel.getImgPath());
            holder.tvTitle.setText(novel.getName());
            holder.tvAuthor.setText(novel.getAuthor());
            holder.tvDesc.setText(novel.getDesc());

            holder.bookView.setiOpenBook(new BookView.IOpenBook() {
                @Override
                public int openBook(View v) {
                    ListView listView = (ListView) v.getParent();
                    int i = listView.indexOfChild(v);
                    List<Novel> zhuda = CityFragment.cityData.getZhudas();
                    Novel novel = zhuda.get(i);

                    // 判断是否登录
                    if (UserFragment.isLogin){
                        //  再判断是有该图书，直接查看shef
                        if (!exitShef(novel.getId())){

                            BookAddRequest bookAddRequest = new BookAddRequest();
                            bookAddRequest.setInter(new BaseRequest.BaseInter<RequestStatus>() {
                                @Override
                                public void handleData(RequestStatus data) {
                                    if (data.getStatus().equals("success")){
                                        //更新shef
                                        iBookRefresh.addBookRresh();
                                    }
                                }
                            });
                            // account ， novelId
                            bookAddRequest.refreshLoad(Constants.getAddBookUrl(UserFragment.USER_ACCOUNT, novel.getId()));
                        }
                    }else {
                        // 跳转到登录页面
                    }

                    return novel.getId();
                }
            });

            return convertView;
        }

        private boolean exitShef(int chooseId) {
            for (Novel novel : ShelfFragment.novels){
                if (novel.getId() == chooseId){
                    return true;
                }
            }
            return false;
        }
    }


    public interface IBookRefresh {
        public void addBookRresh();
    }

    public void setiBookRefresh(IBookRefresh iBookRefresh) {
        this.iBookRefresh = iBookRefresh;
    }

    public IBookRefresh iBookRefresh;


    public class Holder {
        public ImageView iv;
        public TextView tvTitle, tvAuthor, tvDesc;
        private BookView bookView;
    }
}

